package com.yanqu.road.dao.impl.activity.signindraw;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.signindraw.SignInDrawUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class SignInDrawUserDataDaoImpl extends TempDataObjectDao<SignInDrawUserData> {


    public static String getSignInDrawUserDataSql = "select * from `t_u_sign_in_draw_data` where `activityId`=? and `userId`=?";
    public SignInDrawUserData getSignInDrawUserData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getSignInDrawUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateSignInDrawUserDataSql = "update `t_u_sign_in_draw_data` set signDay=?,freeTimes=?,todaySign=?,todayShare=?,resetTime=?,activeDay=?,reward=?,todayOnline=?,todayOnlineTime=?,onlineTimingStart=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(SignInDrawUserData signInDrawUserData) {
        signInDrawUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, signInDrawUserData.getSignDay()));
        para.put(i++, new DbParameter(Types.INTEGER, signInDrawUserData.getFreeTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, signInDrawUserData.getTodaySign()));
        para.put(i++, new DbParameter(Types.INTEGER, signInDrawUserData.getTodayShare()));
        para.put(i++, new DbParameter(Types.INTEGER, signInDrawUserData.getResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, signInDrawUserData.getActiveDay()));
        para.put(i++, new DbParameter(Types.VARCHAR, signInDrawUserData.getReward()));
        para.put(i++, new DbParameter(Types.INTEGER, signInDrawUserData.getTodayOnline()));
        para.put(i++, new DbParameter(Types.INTEGER, signInDrawUserData.getTodayOnlineTime()));
        para.put(i++, new DbParameter(Types.INTEGER, signInDrawUserData.getOnlineTimingStart()));
        para.put(i++, new DbParameter(Types.INTEGER, signInDrawUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, signInDrawUserData.getUserId()));
        if (getDb().executeNoneQuery(updateSignInDrawUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSignInDrawUserDataSql = "insert into `t_u_sign_in_draw_data`(activityId,userId,signDay,freeTimes,todaySign,todayShare,resetTime,activeDay,reward,todayOnline,todayOnlineTime,onlineTimingStart) values (?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(SignInDrawUserData signInDrawUserData) {
        signInDrawUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, signInDrawUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, signInDrawUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, signInDrawUserData.getSignDay()));
        para.put(i++, new DbParameter(Types.INTEGER, signInDrawUserData.getFreeTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, signInDrawUserData.getTodaySign()));
        para.put(i++, new DbParameter(Types.INTEGER, signInDrawUserData.getTodayShare()));
        para.put(i++, new DbParameter(Types.INTEGER, signInDrawUserData.getResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, signInDrawUserData.getActiveDay()));
        para.put(i++, new DbParameter(Types.VARCHAR, signInDrawUserData.getReward()));
        para.put(i++, new DbParameter(Types.INTEGER, signInDrawUserData.getTodayOnline()));
        para.put(i++, new DbParameter(Types.INTEGER, signInDrawUserData.getTodayOnlineTime()));
        para.put(i++, new DbParameter(Types.INTEGER, signInDrawUserData.getOnlineTimingStart()));
        if (getDb().executeNoneQuery(insertSignInDrawUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected SignInDrawUserData parseData(ResultSet rs) throws SQLException {
        SignInDrawUserData signInDrawUserData = new SignInDrawUserData();
        signInDrawUserData.setActivityId(rs.getInt("activityId"));
        signInDrawUserData.setUserId(rs.getLong("userId"));
        signInDrawUserData.setSignDay(rs.getInt("signDay"));
        signInDrawUserData.setFreeTimes(rs.getInt("freeTimes"));
        signInDrawUserData.setTodaySign(rs.getInt("todaySign"));
        signInDrawUserData.setTodayShare(rs.getInt("todayShare"));
        signInDrawUserData.setResetTime(rs.getInt("resetTime"));
        signInDrawUserData.setActiveDay(rs.getInt("activeDay"));
        signInDrawUserData.setReward(rs.getString("reward"));
        signInDrawUserData.setTodayOnline(rs.getInt("todayOnline"));
        signInDrawUserData.setTodayOnlineTime(rs.getInt("todayOnlineTime"));
        signInDrawUserData.setOnlineTimingStart(rs.getInt("onlineTimingStart"));

        return signInDrawUserData;
    }
}
